import { WATERMARK_PARAM } from '~/composables/constant'

export function safeJSONParse(json: string, defalutValue = {}): object | Array<any> {
  if ((!json))
    return defalutValue

  let jsonObj = defalutValue
  try {
    jsonObj = json ? JSON.parse(json) : {}
  }
  catch (error) {
    jsonObj = defalutValue
  }
  return jsonObj
}

export function get(obj: any, prop: string, locale = 'zh') {
  if (!obj || typeof obj !== 'object' || !prop || !['number', 'string'].includes(typeof prop))
    return undefined
  let key = prop
  if (!locale || locale !== 'zh')
    key = `${prop}_${locale}`

  return obj[key]
}
export function replaceImgUrl(path: string) {
  const oldUrl = /http:\/\/chaoyi-cms-api.jiuhuizhan.com/g

  return path?.replace(oldUrl, 'https://chaoyisteel.oss-cn-guangzhou.aliyuncs.com').replace(/.png/, `.png${WATERMARK_PARAM}`).replace(/.jpg/, `.jpg${WATERMARK_PARAM}`)
}
